package cn.zmall.front.controller;

import cn.zmall.common.pojo.Result;
import cn.zmall.common.utils.ResultUtil;
import cn.zmall.manager.dto.OrderDto;
import cn.zmall.manager.dto.ThanksDto;
import cn.zmall.manager.dto.front.Order;
import cn.zmall.manager.dto.front.OrderList;
import cn.zmall.manager.dto.front.OrderRedis;
import cn.zmall.manager.pojo.TbMember;
import cn.zmall.manager.pojo.TbOrder;
import cn.zmall.sso.service.OrderService;
import com.alibaba.dubbo.config.annotation.Reference;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;

/**
 * @Author cxy
 * @Date 2020/11/5 22:16
 * @Version 1.0
 */
@RestController
@Api(value = "订单操作")
public class OrderController {
    @Reference
    private OrderService orderService;

    @RequestMapping(value = "/item/preorder", method = RequestMethod.POST)
    @ApiOperation(value = "创建订单")
    public Result<Object> preOrder(@RequestBody OrderDto orderDto) {
        Long order = orderService.placeOrder(orderDto);
        return new ResultUtil<Object>().setData(order.toString());
    }

    @RequestMapping(value = "/item/getorder", method = RequestMethod.GET)
    @ApiOperation(value = "通过ID获取订单")
    public Result<OrderList> getOrder(String orderId) {
        OrderList result = orderService.getOrder(Long.valueOf(orderId));
        return new ResultUtil<OrderList>().setData(result);
    }

    @RequestMapping(value = "/item/cancelorder", method = RequestMethod.POST)
    @ApiOperation(value = "取消订单")
    public Result<Object> cancelOrder(String orderId) {
        int result = orderService.cancleOrder(Long.valueOf(orderId));
        return new ResultUtil<Object>().setData(result);
    }

    @RequestMapping(value = "/item/deleteorder", method = RequestMethod.DELETE)
    @ApiOperation(value = "删除订单")
    public Result<Object> deleteOrder(String orderId) {
        int result = orderService.deleteOrder(Long.valueOf(orderId));
        return new ResultUtil<Object>().setData(result);
    }

    @RequestMapping(value = "/item/deleteordershopping", method = RequestMethod.DELETE)
    @ApiOperation(value = "删除购物记录")
    public Result<Object> deleteOrderShopping(String orderId) {
        int result = orderService.deleteOrderShopping(Long.valueOf(orderId));
        return new ResultUtil<Object>().setData(result);
    }

    @RequestMapping(value = "/item/payorder", method = RequestMethod.POST)
    @ApiOperation(value = "支付订单")
    public Result<Object> payOrder(@RequestBody ThanksDto thanksDto) {
        int result = orderService.payOrder(thanksDto);
        return new ResultUtil<Object>().setData(result);
    }

    @RequestMapping(value = "/item/usercharge", method = RequestMethod.POST)
    @ApiOperation(value = "用户充值")
    public Result<Object> UserCharge(@RequestParam(defaultValue = "") Long userID,@RequestParam(defaultValue = "") BigDecimal money) {
        int result = orderService.UserCharge(userID, money);
        return new ResultUtil<Object>().setData(result);
    }

    @RequestMapping(value = "/item/getorderlist",method = RequestMethod.GET)
    @ApiOperation(value = "获取用户所有订单")
    public Result<List<TbOrder>> getOrderList(@RequestParam(defaultValue = "") Long userId){
        List<TbOrder> result = orderService.getUserOrderList(userId);
        return new ResultUtil<List<TbOrder>>().setData(result);
    }

    @RequestMapping(value = "/item/getuserunpayment",method = RequestMethod.GET)
    @ApiOperation(value = "获取用户未支付的订单")
    public Result<OrderRedis> getUserUnpayment(@RequestParam(defaultValue = "") Long userId){
        OrderRedis result = orderService.getUserUnpayment(userId);
        return new ResultUtil<OrderRedis>().setData(result);
    }

    @RequestMapping(value = "/item/getaccountpaid",method = RequestMethod.GET)
    @ApiOperation(value = "获取用户已支付的订单")
    public Result<List<TbOrder>> getAccountPaid(@RequestParam(defaultValue = "") Long userId){
        List<TbOrder> result = orderService.getAccountPaid(userId);
        return new ResultUtil<List<TbOrder>>().setData(result);
    }

    @RequestMapping(value = "/item/getorderbystatus",method = RequestMethod.GET)
    @ApiOperation(value = "获取用户不同状态的订单")
    public Result<List<TbOrder>> getOrderByStatus(@RequestParam(defaultValue = "") Long userId,@RequestParam(defaultValue = "") Integer status){
        List<TbOrder> result = orderService.getOrderByStatus(userId, status);
        return new ResultUtil<List<TbOrder>>().setData(result);
    }
}
